package com.v9.main.controller.flow;

import com.v9.common.web.AbstractController;
import com.v9.flow.domain.PageInfo;
import com.v9.flow.service.PageInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author lunshi
 */
@RestController
@Tag(name = "工作流列表管理")
@RequestMapping(
        value = {"/fl/flow/page"},
        method = {RequestMethod.POST}
)
@Slf4j
public class PageInfoController extends AbstractController {
    @Resource
    private PageInfoService pageInfoService;

    @RequestMapping({"/view/{id}"})
    @Operation(summary = "列表")
    public PageInfo view(@PathVariable Long id) {
        return pageInfoService.findById(id);
    }

    @RequestMapping({"/detail"})
    @Operation(summary = "列表")
    public PageCheckResultDto detail(@RequestBody PageRemoveDto dto) {
        PageInfo query = new PageInfo();
        PageCheckResultDto result = new PageCheckResultDto();
        query.setFlowId(dto.getFlowId());
        query.setTaskId(dto.getTaskId());
        PageInfo page = pageInfoService.findByOne(query);
        result.setInfo(page);
        result.setExist(false);
        if (page != null) {
            result.setExist(true);
        }
        return result;
    }

    @RequestMapping({"save"})
    @Operation(summary = "列表")
    public void save(@RequestBody PageInfo info) {
        pageInfoService.saveOrInsert(info);
    }


    @RequestMapping({"remove"})
    @Operation(summary = "列表")
    public void remove(@RequestBody PageRemoveDto dto) {
        PageInfo query = new PageInfo();
        query.setFlowId(dto.getFlowId());
        query.setTaskId(dto.getTaskId());
        PageInfo page = pageInfoService.findByOne(query);
        pageInfoService.remove(page.getId());
    }

    @Data
    public static class PageRemoveDto {
        private Long flowId;
        private String taskId;
    }

    @Data
    public static class PageCheckResultDto {
        private PageInfo info;
        private Boolean exist;
    }
}
